t = int(input())
while t != 0:
    t -= 1
    a, n = map(int, input().split())
    s = 1
    for i in range(1, a + 1):
        s *= i
        pass
    result = 0
    while s != 0:
        if s % 10 == n:
            result += 1
            pass
        s = s // 10
        pass
    print(result)
    pass